package com.sens.mybank;

import android.content.Context;
import android.os.Bundle;

import com.sens.common.ApplicationUtil;
import com.sens.common.Base64;
import com.sens.common.BaseHooker;
import com.sens.common.Config;
import com.sens.common.Log;
import com.sens.database.LiteOrmDBUtil;
import com.sens.ipc.mybank.IPCHelper;
import com.sens.ipc.mybank.application.Handle;
import com.sens.ipc.mybank.hooker.Callback;
import com.sens.mybank.utils.OrderHelper;
import com.sens.mybank.utils.SupportVersion;
import com.sens.network.ApiByHttp;
import com.sens.tables.CommonConfig;
import com.sens.tables.MybankConfig;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class MybankHooker extends BaseHooker implements Callback {
    private Handle handle;
    private boolean hasHook;
    private OrderHelper orderHelper;

    public MybankHooker() {
        super(new String(Base64.decode("Y29tLm15YmFuay5hbmRyb2lkLnBob25l")));
    }

    private XC_MethodHook b25DcmVhdGUHooker = new XC_MethodHook() {
        @Override
        protected void afterHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
            super.afterHookedMethod(param);
            Context context = ((Context) param.thisObject).getApplicationContext();
            Log.i("网商version:" + ApplicationUtil.getVersionName(context));
            if (hasHook) return;
            hasHook = true;
            if (SupportVersion.checkSupportable(ApplicationUtil.getVersionName(context))) {
                if (handle != null) handle.unBind();
                Log.i("hook网商银行");
                Logger.setDebug(Config.ISDEBUG);
                NoHttp.initialize(context);
                handle = IPCHelper.getApplicationHandle(context, MybankHooker.this);
                orderHelper = new OrderHelper(context, handle);
                CommonConfig commonConfig = LiteOrmDBUtil.getGlobalDBUtil(context).queryOne(CommonConfig.class);
                if (commonConfig == null) {
                    commonConfig = new CommonConfig();
                    LiteOrmDBUtil.getGlobalDBUtil(context).save(commonConfig);
                } else {
                    ApiByHttp.getInstance().setHost(commonConfig.getServiceHost());
                }
                MybankConfig mybankConfig = LiteOrmDBUtil.getGlobalDBUtil(context).queryOne(MybankConfig.class);
                if (mybankConfig == null) {
                    mybankConfig = new MybankConfig();
                    LiteOrmDBUtil.getGlobalDBUtil(context).save(mybankConfig);
                }
                ApiByHttp.getInstance().setChannelNumber(mybankConfig.getChannelNumber());
                ApiByHttp.getInstance().setQueryTimeInterval(mybankConfig.getQueryTimeInterval());
                orderHelper.setCollectionAble(mybankConfig.isCollectionAble());
                if (mybankConfig.isStart())
                    orderHelper.start();
            }
        }
    };

    @Override
    public void handlePackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (lpparam.isFirstApplication)
            XposedHelpers.findAndHookMethod(lpparam.classLoader.loadClass(new String(Base64.decode("Y29tLm15YmFuay5hbmRyb2lkLnBob25lLk1haW5BY3Rpdml0eQ"))), new String(Base64.decode("b25DcmVhdGU")), Bundle.class, b25DcmVhdGUHooker);
    }

    @Override
    public void queryOrder() {
        if (orderHelper == null) return;
        orderHelper.justQueryOrder();
    }

    @Override
    public void setChannelNumber(String channelNumber) {
        ApiByHttp.getInstance().setChannelNumber(channelNumber);
    }

    @Override
    public void setQueryTimeInterval(String queryTimeInterval) {
        ApiByHttp.getInstance().setQueryTimeInterval(queryTimeInterval);
    }

    @Override
    public void start() {
        if (orderHelper == null) return;
        MybankConfig mybankConfig = LiteOrmDBUtil.getGlobalDBUtil(orderHelper.getContext()).queryOne(MybankConfig.class);
        if (mybankConfig == null) return;
        orderHelper.setCollectionAble(mybankConfig.isCollectionAble());
        orderHelper.start();
    }

    @Override
    public void stop() {
        if (orderHelper == null) return;
        orderHelper.stop();
    }

    @Override
    public void checkUserId() {
            handle.updateUserId(orderHelper.getUserId());
    }

    @Override
    public void setHost(String host) {
        ApiByHttp.getInstance().setHost(host);
    }
}
